import mysql from 'mysql2/promise';

// 数据库配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: 'Gg1208361409',
  database: 'aiteach_db'
};

// 创建连接池
export const pool = mysql.createPool(dbConfig);

// 视频状态类型
export type VideoStatus = 'pending' | 'processing' | 'completed' | 'failed';

// 视频记录接口
export interface VideoRecord {
  video_id: string;
  status: VideoStatus;
  input_path: string;
  output_path: string | null;
  created_at: Date;
  updated_at: Date;
}

// 创建视频表
export const createVideoTable = async () => {
  const connection = await pool.getConnection();
  try {
    await connection.execute(`
      CREATE TABLE IF NOT EXISTS videos (
        video_id VARCHAR(36) PRIMARY KEY,
        status ENUM('pending', 'processing', 'completed', 'failed') NOT NULL DEFAULT 'pending',
        input_path VARCHAR(255) NOT NULL,
        output_path VARCHAR(255),
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
      )
    `);
    console.log('视频表创建成功');
  } catch (error) {
    console.error('创建视频表失败:', error);
    throw error;
  } finally {
    connection.release();
  }
};
